<?php 
///////
// webirc-client - 2013-2020
// (C) Chris Dorman, GPL v3
// https://notabug.org/Pentium44/ircchat
///////

// server.php - used to communicate between web frontend and irc client
// Grabs from IRC client output
// Pushes to IRC client input
// Keeps IRC client informed on web frontend connection

// Include PHP config file with server, title, and channel settings
include_once("config.php");

session_start();

if(isset($_GET['msg'])) {
	doLog("Msg information: " . $_GET['msg']);
}

$channel = $_SESSION['cwchat-channel'];

// If we have a message; grab user and content and push to IRC client
if (isset($_GET['msg']) && $_GET['msg']!="" && isset($_GET['nick']) && $_GET['nick']!=""){		
	$nick = stripslashes(htmlentities($_GET['nick'])); // Usernick
	$msg  = urldecode(stripslashes(trim($_GET['msg']))); // User message content
	$line = ""; // start with nothing

	// Seperate message input via space
	$cmd = explode(" ", $msg);
	if($cmd[0]=="/msg") { // If using /msg, push private message
		$prvmsg = array_splice($cmd, 2); // grab private message from string
		$line .= "PRIVMSG" . " " . $cmd[1] . " :"; // set for push
		foreach($prvmsg as $word) {
			// Grab each word in the array after the privmsg username
			$line .= $word . " ";
		}
		$line .= "\n";
	} else if ($cmd[0]=="/join") {
		doLog("$username: channel switch from $channel to" . $cmd[1] . "($msg)");
		$line .= "PART $channel\n"; // push close command to IRC
		$line .= "JOIN" . " " . $cmd[1] . "\n"; // set for push
		$_SESSION['cwchat-channel'] = trim($cmd[1]);
	} else {
		// @@ This is a work in progress
		// Sends every channel message to each channel :[
		$line .= "PRIVMSG $channel :$msg\n";
	}
	
	// Get original content
	$content = file_get_contents(".$nick.socket");
	echo nl2br(stripslashes($content));
	// Grab all contents, and push to socket output file.
	file_put_contents(".$nick.socket", $content . $line);
	// Grab user message and push to IRC client
	file_put_contents(".$nick.push", $line);
	// Throw out your user message
	echo nl2br(stripslashes($line));
	// DONE
		
} else if (isset($_GET['get']) && isset($_GET['nick']) && $_GET['nick']!="") {
	$nick = stripslashes(htmlentities($_GET['nick'])); // Username
	// Grab IRC client output
	$content = file_get_contents(".$nick.socket");
	// Push content to the web frontend
	echo nl2br(htmlentities(stripslashes($content)));
	// DONE
} else if (isset($_GET['do']) && isset($_GET['nick']) && $_GET['nick']!="") {
	$nick = stripslashes(htmlentities($_GET['nick']));
	if($_GET['do']=="login") { // Is user asking for login?
		// Join channel
		if(!isset($_SESSION['cwchat-channel'])) {
			file_put_contents(".$nick.push", "JOIN " . $default_channel . "\n");
		} else {
			file_put_contents(".$nick.push", "JOIN " . $channel . "\n");
		}
		
		// Make sure users DB is clean, put nothing into socket read file
		file_put_contents(".$nick.socket", "");
		chmod(".$username.socket", 0755); // file permissions for read / write
		
		// start pingfile - determines if webclient is active via write timestamp 
		file_put_contents(".$nick.pingfile", "pong");
		chmod(".$username.pingfile", 0755); // file permissions for read / write
		
		// Execute IRC client in background
		// IRC server will die when either 1) pingfile is deleted, or
		// 2) if pingfile is older than 10 seconds of current sys time
		$realpath = realpath("./irc.php"); // get full file path
		
		// Execute IRC client
		shell_exec("/usr/bin/php $realpath $nick > /dev/null 2>/dev/null &"); 
	} else if($_GET['do']=="logout") { // Is user asking for logout?
		// Remove ping file if user logs out. IRC server will close
		unlink(".$nick.pingfile");
	} else if($_GET['do']=="keepup") { // Client asking for keepup ping.
		// PONG to server.
		file_put_contents(".$nick.pingfile", "ping");
	}
}
?>	
